package com.zhufei.dao;


import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

public class DBCon {
	public  static Connection con = null;
	public  static Statement st = null;

	public DBCon() {
		// String path = this.getClass().getResource("").getPath();
		// System.out.println(path);
		try {
			/*
			 * Class.forName(dbInfoDatas.dbDriver); con =
			 * DriverManager.getConnection(dbInfoDatas.dbUrl,
			 * dbInfoDatas.dbUsername, dbInfoDatas.dbPassword); st =
			 * con.createStatement();
			 */
			/*
			Context ctx = new InitialContext();
			DataSource ds = (DataSource) ctx.lookup("java:comp/env/jdbc/db");
			con = ds.getConnection();
			st = con.createStatement();
*/
			Class.forName("com.mysql.jdbc.Driver");
			con = DriverManager.getConnection( GlobalsDao.database_url, GlobalsDao.database,GlobalsDao.database_password);
			st = con.createStatement();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
//			 }

		}

	}

	public static void closeDB() {
		if (st != null) {
			try {
				st.close();
				System.out.println("close the DBCon st");
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (con != null) {
			try {
				con.close();
				System.out.println("close the DBCon con");
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	public Connection getConnection() {
		return this.con;
	}

	public Statement getStatement() {
		return this.st;
	}

}
